import { reactive, onMounted } from "vue";
import axios, { AxiosError } from "axios";
export default function () {
  let dogList = reactive<string[]>([]);//初始化dogList数组
  // 方法
  async function getDog() {
    try {
      // 发请求
      let { data } = await axios.get(
        "https://dog.ceo/api/breed/pembroke/images/random"
      );
      // 维护数据
      console.log(data)//{message: 'https://images.dog.ceo/breeds/pembroke/n02113023_894.jpg', status: 'success'}
      dogList.push(data.message);//作为图片list
    } catch (error) {
      // 处理错误
      const err = <AxiosError>error;
      console.log(err.message);
    }
  }
  // 挂载钩子
  onMounted(() => {
    getDog();
  });
  // 向外部暴露数据
  return { dogList, getDog };
}
